var deviceCtrl = avalon.define({
	$id : "deviceStats",
	staffId : '',
	param : {
		regionCode : "",
		storeCode : "",
		bindStatus : "",
		staffId : "",
		search : function() {
			deviceTable.search({
				regionCode : deviceCtrl.param.regionCode,
				storeCode : deviceCtrl.param.storeCode,
				bindStatus : deviceCtrl.param.bindStatus,
				staffId : deviceCtrl.param.staffId
			});
		}
	},
	popup : {
		unbind : false,
		unbindSure : function() {
			deviceCtrl.popup.unbind = false;
			$.ajax({
				url : DI.unBindDevice,
				type : 'post',
				data: {
                    staffId: deviceCtrl.staffId,
                },
                dataType: "json",
                success: function(data){
                	if(data.retFlag == 1){
                        PLATFORM.headTip("操作成功", "#53CC2D");
                        deviceTable.refresh();
                    }else{
                        PLATFORM.headTip(data.errorMessage);
                    }
                	deviceCtrl.popup.loading = false;
                }
			});
		},
		bind : false,
		bindSure : function() {
			deviceCtrl.popup.loading = true;
			deviceCtrl.popup.bind = false;
			$.ajax({
				url : DI.bindDevice,
				type : 'post',
				data: {
                    staffId: deviceCtrl.staffId,
                },
                dataType: "json",
                success: function(data){
                	if(data.retFlag == 1){
                        PLATFORM.headTip("操作成功", "#53CC2D");
                        deviceTable.refresh();
                    }else{
                        PLATFORM.headTip(data.errorMessage);
                    }
                	deviceCtrl.popup.loading = false;
                },
                error : function() {
                	deviceCtrl.popup.loading = false;
                }
			});
		},
		close : function() {
			deviceCtrl.popup.unbind = deviceCtrl.popup.bind = false;
		},
		loading: false,
        loadingTxt: ""
	},
	loginLogShow: false,
    back : function() {
		deviceCtrl.loginLogShow = false;
	}
});
avalon.scan();
avalon.filters.isBind = function(isBind){
    return isBind == '1' ? "已绑定" : "未绑定";
};
$(function() {
	var magicRegion = $('#magicRegion').magicSuggest({
		placeholder : "选择大区",
		required : true,
		selectionStacked : true,
		expandOnFocus : true
	});
	$(magicRegion).on("selectionchange", function(e, self, records) {
		if (records.length > 0) {
			deviceCtrl.param.region = records[0].name;
			deviceCtrl.param.regionCode = records[0].id;
			$.ajax({
				type : "post",
				url : DI.queryStroeByRegionCode,
				data : {
					regionCode : records[0].id
				},
				dataType : "json",
				success : function(shops) {
					deviceCtrl.allShop = shops.storeInfos;
					var allShopData = [];
					for ( var j = 0; j < deviceCtrl.allShop.length; j++) {
						allShopData.push({
							id : deviceCtrl.allShop[j].storeCode,
							name : deviceCtrl.allShop[j].storeName
						});
					}
					magicShop.clear();
					magicShop.collapse();
					magicShop.setData(allShopData);
					magicShop.enable();
				},
				error : function() {

				}
			});
		} else {
			deviceCtrl.param.regionCode = "";
			deviceCtrl.param.storeCode = "";
			magicShop.clear();
			magicShop.collapse();
			magicShop.setData([]);
			magicShop.disable();
		}
	});
	var magicShop = $('#magicShop').magicSuggest({
		placeholder : "选择门店",
		required : true,
		selectionStacked : true,
		disabled : true,
		expandOnFocus : true
	});
	$(magicShop).on("selectionchange", function(e, self, records) {
		if (records.length > 0) {
			deviceCtrl.param.shop = records[0].name;
			deviceCtrl.param.storeCode = records[0].id;
		} else {
			deviceCtrl.param.shop = "";
			deviceCtrl.param.storeCode = "";
		}
	});
	var bindStatus = $('#bindStatus').magicSuggest({
		placeholder : "选择绑定状态",
		required : true,
		selectionStacked : true,
		expandOnFocus : true
	});
	$(bindStatus).on("selectionchange", function(e, self, records) {
		if (records.length > 0) {
			deviceCtrl.param.bindStatusStr = records[0].name;
			deviceCtrl.param.bindStatus = records[0].id;
		} else {
			deviceCtrl.param.shop = "";
			deviceCtrl.param.storeCode = "";
		}
	});
	$.ajax({
		type : "post",
		url : DI.queryAllRegion,
		dataType : "json",
		success : function(data) {
			deviceCtrl.allRegion = data.regions;
			var allRegionData = [];
			allRegionData.push({id:'-1', name:'总部'});
			for ( var i = 0; i < deviceCtrl.allRegion.length; i++) {
				allRegionData.push({
					id : deviceCtrl.allRegion[i].regionCode,
					name : deviceCtrl.allRegion[i].regionName
				});
			}
			magicRegion.setData(allRegionData);
			magicRegion.setValue([ allRegionData[0].id ]);
			// 初始化绑定状态选项
			var allBindStatusData = [];
			allBindStatusData.push({id:0, name:"未绑定"});
			allBindStatusData.push({id:1, name:"已绑定"});
			bindStatus.setData(allBindStatusData);
			bindStatus.setValue([ allBindStatusData[0].id ]);
			
			var opts_device = {
				targetId : "deviceTb",
				templateId : "deviceTbTemplate",
				settings : {
					url : DI.queryBindDevices,
					ajaxType : "json",
					ajaxDataHandler : function(data) {
						var temp = {};

						if (data.retFlag == "1" && data.data) {
							temp.total = data.data.totalSize;
							temp.rows = data.data.records;
						} else {
							if (data.errorMessage && data.errorMessage != "") {
								PLATFORM.headTip(data.errorMessage);
							}
							temp.total = 0;
							temp.rows = [];
						}

						return temp;
					},
					paginationIndexName : {
						"peerpageRows" : 'pageSize',
						"currentPage" : 'pageNo'
					},
					searchParam: {
						regionCode : deviceCtrl.param.regionCode,
						storeCode : deviceCtrl.param.storeCode,
						bindStatus : deviceCtrl.param.bindStatus,
						staffId : deviceCtrl.param.staffId
					}
				},
				customFuncs : {
					unbind : function(el) {
						deviceCtrl.popup.unbind = true;
						deviceCtrl.staffId = el.staffId;
					},
					bind : function(el) {
						$.ajax({
							url : DI.queryLoginLogs,
							type : 'POST',
							data : {staffId:el.staffId, pageNo:1, pageSize:1},
							success : function(data) {
								if(data.retFlag == 1){
									if (data.data.totalSize > 0) {
										deviceCtrl.popup.bind = true;
										deviceCtrl.staffId = el.staffId;
										$('#bindStaffId').val(el.staffId);
										$('#bindDeviceId').val(data.data.records[0].deviceId);
										$('#bindDeviceName').val(data.data.records[0].deviceName);
									} else {
										PLATFORM.headTip("未能从登录日志中查到相应设备ID");
									}
			                    }else{
			                        PLATFORM.headTip(data.errorMessage);
			                    }
							}
						});
					},
					loginHistory : function(el) {
						deviceCtrl.loginLogShow = true;
						opts_history.settings.searchParam.staffId = el.staffId;
	                    //如果没初始化就初始化 初始化了就刷新
	                    if(historyTable){                                                                
	                        historyTable.refresh();
	                    }else{
	                        historyTable = SwiftUI.swiftTable.getInstance(opts_history);
	                    }
					}
				}
			},
			opts_history = {
				targetId: "loginTb",
	            templateId: "loginTbTemplate",
	            settings: {
	                url: DI.queryLoginLogs,
	                ajaxType: "json",
	                searchParam: {staffId: ""},
	                ajaxDataHandler: function(data){
	                    var temp = {};
	                    temp.total = data.data.totalSize;
	                    if(data.data){
	                        temp.rows = data.data.records;
	                    }else{
	                        temp.rows = [];
	                        throw "数据结构错误，缺少rows参数，请检查返回值";
	                    }
	                    
	                    return temp;
	                },
	                paginationIndexName:{
	                    "peerpageRows":'pageSize',
	                    "currentPage":'pageNo'
	                },
	            }
			};
			deviceTable = SwiftUI.swiftTable.getInstance(opts_device);
			historyTable = null;
		},
		error : function(err) {
			console.log(err);
		}
	});
});